Page({
  data: {
    prize: null
  },

  onLoad: function (options) {
    if (options.prize) {
      try {
        // 解码并解析 JSON 字符串
        const prize = JSON.parse(decodeURIComponent(options.prize));
        this.setData({ prize: prize });
      } catch (e) {
        console.error('Failed to parse prize:', e);
      }
    }
  },

  onExchangeTap: function(event) {
    const giftId = event.currentTarget.dataset.prizeId;

    // 构建请求数据
    const giftSeizeReqDTO = {
      giftId: giftId,
      addressId: 1
    };

    // 发送请求
    wx.request({
      url: 'http://localhost:9020/couponIntegral-service/consumer/gift/seize', // 替换为实际的服务地址
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: giftSeizeReqDTO,
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '兑换成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '兑换失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('兑换请求失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  }
});